package com.ssm.zyn.action;

import com.ssm.zyn.eo.TestTableEOpage;
import com.ssm.zyn.model.TestTable;
import com.ssm.zyn.service.Testtableservice;
import com.ssm.zyn.tool.ResponseMessage;
import com.ssm.zyn.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("testTable")
@RestController
@CrossOrigin
public class TestTableController {
    @Autowired
    private Testtableservice testtableservice;
    @ApiOperation(value = "题库添加方法")
    @RequestMapping(value="/addTestTable",method = RequestMethod.GET)
    public ResponseMessage addTestTable(TestTable testTable){
        int IsAdd=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if(testTable!=null){
            //(2)角色名称不能为空
            if(testTable.getExamcontent()!=null){
                //(3)添加角色数据
                IsAdd=testtableservice.AddTesttable(testTable);
                if(IsAdd==1){
                    return Result.success("保存成功");
                }else if(IsAdd==-1){
                    return Result.error("试题内容重复不允许添加");
                }else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "题库修改方法")
    @RequestMapping(value="/updateTesttable",method = RequestMethod.GET)
    public ResponseMessage updateTesttable(TestTable testTable){
        int isUpdate=0;
        //传入的参数不能为空
        if(testTable!=null){
            //参数当中主键不能为0

            if (testTable.getQid()!=0){
                isUpdate=testtableservice.UpdateTesttable(testTable);
                if(isUpdate==1){

                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
            }
        }
        return  Result.error("传入参数不正确");
    }
    @ApiOperation(value = "题库删除方法")
    @RequestMapping(value="/delTesttable",method = RequestMethod.GET)
    public ResponseMessage deTesttable(int qid){
        if (qid!=0){
            int delInt=0;
            delInt=testtableservice.DeleteTesttable(qid);
            if(delInt>0){
                return Result.success("删除成功");
            }else {
                return Result.error("与中间表有关联，删除失败");
            }
        }else {
            return Result.error("删除的主键不能为0,请重新输入");
        }
    }
    @ApiOperation(value = "根据条件获取题库信息")
    @RequestMapping(value = "/getTestTableAll",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getTestTableAll(/*@RequestBody*/ TestTableEOpage testTableEOpage){
        Map<String, List> map=new HashMap<>();
        List<TestTableEOpage> listTestTable=testtableservice.getWhereAll(testTableEOpage);
        map.put("list",listTestTable);
        return Result.success("200",map);
    }
}
